package com.zhku.onlineshop.Service.impl;


import com.zhku.onlineshop.Dao.OrderDao;
import com.zhku.onlineshop.Dao.OrderItemDao;
import com.zhku.onlineshop.Pojo.Order;
import com.zhku.onlineshop.Pojo.OrderItem;
import com.zhku.onlineshop.Service.OrderService;
import com.zhku.onlineshop.Servlet.OrderServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderItemDao orderItemDao;

    @Override
    public Integer submitOrder(Order order) {
        // 1. 插入订单并获取生成的ID
        orderDao.insertOrder(order);

        // 2. 插入订单项
        for (OrderItem item : order.getOrder_items()) {
            item.setOrder_id(order.getId()); // 设置订单ID
            orderItemDao.insertOrderItem(item);
        }
        return order.getId();
    }

    @Override
    public boolean changeOrderStatus(Integer id, String status) {
        return  orderDao.updateOrderStatus(id, status);
    }
    @Override
    public boolean removeOrder(Integer id){
        return  orderDao.deleteOrder(id);
    }

    @Override
    public List<Order> getOrdersByCustomerId(Integer customer_id) {
        return orderDao.findOrdersByCustomerId(customer_id);
    }

    @Override
    public List<Order> getOrderBystatus(Integer status) {
        return orderDao.getOrderBystatus(status);
    }
}
